package com.joe.test_mybatis_plus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joe.test_mybatis_plus.entity.TestUser;
import com.joe.test_mybatis_plus.entity.User;
import com.joe.test_mybatis_plus.mapper.UserMapper;
import com.joe.test_mybatis_plus.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/user")
@Api(value = "用户接口")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "查询全部用户",notes = "查询全部用户")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result getList(){
        List<User> users = userMapper.selectList(new QueryWrapper<User>().eq("delete_flag",0));
        return Result.success(users);
    }

    @ApiOperation(value = "保存用户",notes = "保存用户")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result saveUser(@Validated @RequestBody User user){
        int insert = userMapper.insert(user);
        if (insert>0){
            return Result.success(user);
        }
        return Result.fail("保存失败");
    }

    @ApiOperation(value = "用户信息[Validate校验]",notes = "用户信息[Validate校验]")
    @RequestMapping(value = "/saveByValidate",method = RequestMethod.POST)
    public Result saveByValidate(@Validated @RequestBody User user){
        return Result.success(user);
    }

    @ApiOperation(value = "删除用户[逻辑]",notes = "删除用户[逻辑]")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public Result deleteUser(@PathVariable("id")Long id){
        User user = userMapper.selectById(id);
        user.setDeleteFlag(1);
        int i = userMapper.updateById(user);
        if (i>0){
            return Result.success();
        }
        return Result.fail();
    }

    @ApiOperation(value = "删除用户[物理]",notes = "删除用户[物理]")
    @RequestMapping(value = "/delete2/{id}",method = RequestMethod.DELETE)
    public Result deleteUser2(@PathVariable("id")Long id){
        int i = userMapper.deleteById(id);
        if (i>0){
            return Result.success();
        }
        return Result.fail();
    }

    @ApiOperation(value = "分页查询用户",notes = "分页查询用户")
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public Result getListByPage(@RequestParam Integer pageNo,@RequestParam Integer pageSize){
        Page<User> page = new Page<>(pageNo,pageSize);
        Page<User> page1 = userMapper.selectPage(page, new QueryWrapper<User>().eq("delete_flag",0));
        //List<User> records = page1.getRecords();
        return Result.success(page1);
    }

    @ApiOperation(value = "保存测试用户",notes = "保存测试用户")
    @RequestMapping(value = "/saveTestUser",method = RequestMethod.POST)
    public Result saveTestUser(@Validated @RequestBody TestUser user){
        List<Integer> a = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            a.add(i);
        }
        System.out.println(a.get(10));
        return Result.success();
    }

    @ApiOperation(value = "修改测试用户",notes = "修改测试用户")
    @RequestMapping(value = "/updateTestUser",method = RequestMethod.PUT)
    public Result updateTestUser(@Validated @RequestBody TestUser user){
        return Result.success();
    }
}
